<template>
  <div class="container">
    <!-- 路径导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>心理知识管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索区域 -->
    <el-row>
      <el-form :inline="true" :model="queryInfo">
        <el-form-item label="标题">
          <el-input
            v-model="queryInfo.name"
            clearable
            @clear="queryList"
            placeholder="请输入标题名称"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="queryList">搜索</el-button>
          <el-button type="primary" @click="toAddHeartknowledge">新增心理知识</el-button>
          <el-button type="danger" @click="deleteBatch">批量删除</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <!-- 表格区域 -->
    <el-table
      :data="HeartknowledgeList"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="name" label="标题"></el-table-column>
      <el-table-column
        prop="simplyIntroduce"
        label="简介"
      ></el-table-column>
      <el-table-column prop="tabularChart" label="图片">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="
              'http://localhost:8888/sfzy_ssm/yuLan?filename=' +
              scope.row.picture
            "
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-link type="primary" @click="toEditHeartknowledge(scope.row)"
            >编辑心理知识</el-link
          >
          <el-link type="primary" @click="deleteById(scope.row.id)"
            >删除心理知识</el-link
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pageNum"
      :page-sizes="[1, 5, 10, 20]"
      :page-size="queryInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 新增文章和修改文章的对话框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancle">
      <el-form :model="Heartknowledge">
        <el-form-item label="标题" label-width="80px">
          <el-input
            placeholder="请输入标题"
            v-model="Heartknowledge.name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="简介" label-width="80px">
          <el-input
            placeholder="请输入心理知识简介"
            v-model="Heartknowledge.simplyIntroduce"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="图片" label-width="80px">
          <el-input
            placeholder="请输入心理知识图片"
            v-model="Heartknowledge.picture"
            autocomplete="off"
          ></el-input>
          <el-upload
            action="http://localhost:8888/sfzy_ssm/upload"
            list-type="picture-card"
            :on-success="handleSuccess"
            :file-list="fileList"
            :limit="1"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>
        <el-form-item label="详情" label-width="80px">
          <!-- 富文本编辑器组件 -->
          <quill-editor v-model="Heartknowledge.text"></quill-editor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="addHeartknowledge">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      //预览
      fileList: [],
      // 照片上传
      dialogImageUrl: "",
      dialogVisible: false,

      // 保存选中行的数据
      selectedHeartknowledge: [],
      // 新增和修改对话框
      title: "",
      dialogFormVisible: false,
      Heartknowledge: {
        id: "",
        name: "",
        simplyIntroduce: "",
        picture:'',
        text:''
      },
      // 查询信息对象
      queryInfo: {
        name: "",
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      HeartknowledgeList: [],
    };
  },
  mounted() {
    this.queryList();
  },
  methods: {
    //照片上传
    handleSuccess(response) {
      //处理上传成功的钩子函数
      //上传成功后赋值给Heartknowledge
      this.Heartknowledge.picture = response.message;
    },
    handleRemove() {
      //删除图片事件
      this.Heartknowledge.picture = ""; //图片移除后图片路径也要清空
    },
    handlePictureCardPreview(file) {
      //预览图片
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //删除单个
    deleteById(id) {
      this.$confirm("本操作比较危险,是否继续操作", "提示信息", {
        distinguishCancelAndClose: true,
        confirmButtonText: "继续",
        cancelButtonText: "取消",
      })
        .then(() => {
          this.$http.delete("heartknowledge/remove/" + id).then((resp) => {
            if (resp.data.status == 200) {
              this.$message.success({
                message: "删除成功",
                duration: 1000,
                onClose:()=>{
                  this.queryList()
                }
              })
            } else {
              this.$message.error("删除失败!");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //批量删除
    deleteBatch() {
      if (this.selectedHeartknowledge.length > 0) {
        this.$confirm("本操作比较危险,是否继续操作", "提示信息", {
          distinguishCancelAndClose: true,
          confirmButtonText: "继续",
          cancelButtonText: "取消",
        })
          .then(() => {
            let deleteArray = this.selectedHeartknowledge.map((item) => item.id);
            // console.log(deleteArray);
            this.$http
              .delete("heartknowledge/removeBatch?ids=" + deleteArray)
              .then((resp) => {
                if (resp.data.status == 200) {
                  this.$message.success("删除成功!");
                  this.queryList();
                } else {
                  this.$message.error("删除失败!");
                }
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      } else {
        this.$message.warning("请选择数据");
      }
    },
    //处理选中行
    handleSelectionChange(val) {
      this.selectedHeartknowledge = val;
      // console.log(this.selectedArticle);
    },
    // 修改文章对话框展示
    toEditHeartknowledge(row) {
      this.Heartknowledge = row;
      this.title = "编辑心理知识";
      this.dialogFormVisible = true;
      //动态的获取名称并绑定到fileList
      this.fileList = [
        {
          name: this.Heartknowledge.picture,
          url:
            "http://localhost:8888/sfzy_ssm/yuLan?filename=" + row.picture,
        },
      ];
    },
    //调用后端接口添加或修改数据
    addHeartknowledge() {
      if (this.Heartknowledge.id) {
        this.$http.put("heartknowledge/edit", this.Heartknowledge).then((resp) => {
          // console.log(resp);
          if (resp.data.status == 200) {
            this.$message.success("修改成功");
            this.queryList();
            this.cancle();
          }
        });
      } else {
        this.$http.post("heartknowledge/add", this.Heartknowledge).then((resp) => {
          // console.log(resp);
          if (resp.data.status == 200) {
            this.$message.success("添加成功");
            this.queryList();
            this.cancle();
          }
        });
      }
    },
    // 清空数据关闭对话框
    cancle() {
      this.dialogFormVisible = false;
      this.Heartknowledge = {
        id: "",
        name: "",
        simplyIntroduce: "",
        picture:'',
        text:''
      };
      this.fileList = [];
    },
    // 新增对话框展示
    toAddHeartknowledge() {
      this.dialogFormVisible = true;
      this.title = "新增心理知识";
    },
    // 分页查询信息的方法
    queryList() {
      // 调用后端接口来获取数据
      this.$http
        .get("heartknowledge/find", {
          params: this.queryInfo,
        })
        .then((resp) => {
          if (resp.data.status == 200) {
            // console.log(resp.data);
            this.HeartknowledgeList = resp.data.data.list;
            this.total = resp.data.data.total;
            // console.log(this.articleList);
          } else {
            this.$message.error("查询失败！");
          }
        });
    },

    handleSizeChange(size) {
      //console.log('每页显示数量发生改变');
      this.queryInfo.pageSize = size;
      this.queryInfo.pageNum = 1;
      // 重新查询
      this.queryList();
    },
    handleCurrentChange(num) {
      //console.log('页码发生改变');
      this.queryInfo.pageNum = num;
      // 重新查询
      this.queryList();
    },
  },
};
</script>
<style>
.el-breadcrumb {
  margin-bottom: 15px;
  padding: 0 0 15px;
  border-bottom: 1px solid #ddd;
}

.el-pagination {
  margin-top: 15px;
}

.el-link {
  padding: 0 5px;
  font-size: 12px;
}
/**设置富文本编辑器的最小高度，注意style中不能使用scoped **/
.ql-container {
  min-height: 200px;
}
</style>
